using System;
using System.Net;
using System.ServiceModel.Web;
using System.Text;

namespace Catchbase.Service
{
    public class SecurityManager
    {
        public static void Authorize()
        {
            if (WebOperationContext.Current == null)
                throw new WebFaultException(HttpStatusCode.InternalServerError);

            var value = WebOperationContext.Current.IncomingRequest.Headers.Get("Authorization");
            if (value == null || value.Length <= 6)
                throw new WebFaultException(HttpStatusCode.Unauthorized);

            var token = value.Substring(6);
            var values = Encoding.UTF8.GetString(Convert.FromBase64String(token));
            var pair = values.Split(new [] { ":" }, StringSplitOptions.None);
            if(pair.Length != 2)
                throw new WebFaultException(HttpStatusCode.Unauthorized);
            return;
            if (pair[1] != "Load shared key from config?")
                throw new WebFaultException(HttpStatusCode.Unauthorized);
        }
    }
}